home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / xinu.arc / XINU5.C < prev    next >
Text File  |  1986-01-03  |  8KB  |  304 lines

  1. /* ttyoin.c - ttyoin  p. 172 */
  2.  
  3. # include    <conf.h>
  4. # include    <kernel.h>
  5. # include    <tty.h>
  6. # include    <io.h>
  7. # include    <slu.h>
  8.  
  9. /*-----------------------------------------------------------------------------
  10.  *  ttyoin  --  lower-half tty device driver for output interrupts
  11.  *-----------------------------------------------------------------------------
  12.  */
  13. INTPROC    ttyoin(iptr)
  14.     register struct tty    *iptr;
  15. {
  16.     register struct csr    *cptr;
  17.     int    ct;
  18.  
  19.     cptr = iptr->ioaddr;
  20.     if (iptr->ehead != iptr->etail)  {
  21.         cptr->ctbuf = iptr->ebuff[iptr->etail++];
  22.         if (iptr->etail >= EBUFLEN)
  23.             iptr->etail = 0;
  24.         return;
  25.     }
  26.     if (iptr->oheld)  {        /* honor flow control                */
  27.         cptr->ctstat = SLUDISABLE;
  28.         return;
  29.     }
  30.     if ((ct=scount(iptr->osem)) < OBUFLEN)  {
  31.         cptr->ctbuf = iptr->obuff[iptr->otail++];
  32.         if (iptr->otail >= OBUFLEN)
  33.             iptr->otail = 0;
  34.         if (ct > OBMINSP)
  35.             signal(iptr->osem);
  36.         else if (++(iptr->odsend) == OBMINSP)  {
  37.             iptr->odsend = 0;
  38.             signaln(iptr->osem, OBMINSP);
  39.         }
  40.     }  else
  41.         cptr->ctstat = SLUDISABLE;
  42. }
  43. /* ttyputc.c - ttyputc  p. 167 */
  44.  
  45. # include    <conf.h>
  46. # include    <kernel.h>
  47. # include    <tty.h>
  48. # include    <io.h>
  49. # include    <slu.h>
  50.  
  51. /*-----------------------------------------------------------------------------
  52.  *  ttyputc --  write one character to a tty device
  53.  *-----------------------------------------------------------------------------
  54.  */
  55. ttyputc(devptr, ch)
  56.     struct devsw    *devptr;
  57.     char    ch;
  58. {
  59.     struct tty    *iptr;
  60.     char    ps;
  61.  
  62.     iptr = &tty[devptr->dvminor];
  63.     if (ch == NEWLINE && iptr->ocrlf)
  64.         ttyputc(devptr,RETURN);
  65.     wait(iptr->osem);        /* wait for space in queue           */
  66.     disable(ps);
  67.     iptr->obuff[iptr->ohead++] = ch;
  68.     if (iptr->ohead >= OBUFLEN)
  69.         iptr->ohead = 0;
  70.     (iptr->ioaddr)->ctstat = SLUENABLE;
  71.     restore(ps);
  72.     return(OK);
  73. }
  74. /* ttyreac.c - ttyread, readcopy  p. 165 */
  75.  
  76. # include    <conf.h>
  77. # include    <kernel.h>
  78. # include    <tty.h>
  79. # include    <io.h>
  80. # include    <slu.h>
  81.  
  82. /*-----------------------------------------------------------------------------
  83.  *  ttyread  --  read one or more characters from a tty device
  84.  *-----------------------------------------------------------------------------
  85.  */
  86. ttyread(devptr, buff, count)
  87.     struct devsw    *devptr;
  88.     int    count;
  89.     char    *buff;
  90. {
  91.     char    ps;
  92.     register struct tty    *iptr;
  93.     int    avail, nread;
  94.  
  95.     if (count < 0)
  96.         return(SYSERR);
  97.     disable(ps);
  98.  
  99.     avail = scount ((iptr=&tty[devptr->dvminor])->isem);
  100.     if ((count = (count==0 ? avail : count)) == 0)  {
  101.         restore(ps);
  102.         return(0);
  103.     }
  104.     nread = count;
  105.     if (count <= avail)
  106.         readcopy(buff, iptr, count);
  107.     else  {
  108.         if (avail > 0)  {
  109.             readcopy(buff, iptr, avail);
  110.             buff += avail;
  111.             count -= avail;
  112.         }
  113.         for ( ; count>0 ; count-- )
  114.             *buff++ = ttygetc(devptr);
  115.     }
  116.     restore(ps);
  117.     return(nread);
  118. }
  119.  
  120. /*-----------------------------------------------------------------------------
  121.  *  readcopy  --  high speed copy procedure used by ttyread
  122.  *-----------------------------------------------------------------------------
  123.  */
  124. LOCAL    readcopy(buff, iptr, count)
  125.     register char    *buff;
  126.     struct tty    *iptr;
  127.     int    count;
  128. {
  129.     register char    *qtail, *qend, *uend;    /* copy loop variables       */
  130.  
  131.     qtail = &iptr->ibuff[iptr->itail];
  132.     qend  = &iptr->ibuff[IBUFLEN];
  133.     uend  = buff + count;
  134.     while ( buff < uend )  {
  135.         *buff++ = *qtail++;
  136.         if ( qtail >= qend )
  137.             qtail = iptr->ibuff;
  138.     }
  139.     iptr->itail = qtail - iptr->ibuff;
  140.     sreset(iptr->isem, scount(iptr->isem)-count);
  141. }
  142. /* ttywrite.c - ttywrite, writcopy  p. 170 */
  143.  
  144. # include    <conf.h>
  145. # include    <kernel.h>
  146. # include    <tty.h>
  147. # include    <io.h>
  148. # include    <slu.h>
  149.  
  150. /*-----------------------------------------------------------------------------
  151.  *  ttywrite  --  write one or more characters to a tty device
  152.  *-----------------------------------------------------------------------------
  153.  */
  154. ttywrite(devptr, buff, count)
  155.     struct devsw    *devptr;
  156.     char    *buff;
  157.     int    count;
  158. {
  159.     register struct tty    *ttyp;
  160.     int    avail;
  161.     char    ps;
  162.  
  163.     if (count < 0)
  164.         return(SYSERR);
  165.     if (count == 0)
  166.         return(OK);
  167.     disable(ps);
  168.     ttyp = &tty[devptr->dvminor];
  169.     if ((avail=scount(ttyp->osem)) >= count)  {
  170.         writcopy(buff, ttyp, count);
  171.         (ttyp->ioaddr)->ctstat = SLUENABLE;
  172.     }  else  {
  173.         if (avail > 0)  {
  174.             writcopy(buff, ttyp, avail);
  175.             buff += avail;
  176.             count -= avail;
  177.         }
  178.         for ( ; count>0 ; count-- )
  179.             ttyputc(devptr, *buff++);
  180.     }
  181.     restore(ps);
  182.     return(OK);
  183. }
  184.  
  185. /*-----------------------------------------------------------------------------
  186.  *  writcopy  --  highspeed copy from user's buffer into system buffer
  187.  *-----------------------------------------------------------------------------
  188.  */
  189. LOCAL    writcopy(buff, ttyp, count)
  190.     register char    *buff;
  191.     struct tty    *ttyp;
  192.     int    count;
  193. {
  194.     register char    *qhead, *qend, *uend;
  195.  
  196.     qhead = &ttyp->obuff[ttyp->ohead];
  197.     qend  = &ttyp->obuff[OBUFLEN];
  198.     uend  = buff + count;
  199.     while (buff < uend)  {
  200.         *qhead++ = *buff++;
  201.         if (qhead >= qend)
  202.             qhead = ttyp->obuff;
  203.     }
  204.     ttyp->ohead = qhead - ttyp->obuff;
  205.     sreset(ttyp->osem, scount(ttyp->osem)-count);
  206. }
  207. /* userret.c - userret  p. 76 */
  208.  
  209. # include    <conf.h>
  210. # include    <kernel.h>
  211.  
  212. /*-----------------------------------------------------------------------------
  213.  * userret  --  entered when a process exits by return
  214.  *-----------------------------------------------------------------------------
  215.  */
  216. userret()
  217. {
  218.     kill( getpid() );
  219. }
  220. /* wait.c - wait  p. 85 */
  221.  
  222. # include    <conf.h>
  223. # include    <kernel.h>
  224. # include    <proc.h>
  225. # include    <q.h>
  226. # include    <sem.h>
  227.  
  228. /*-----------------------------------------------------------------------------
  229.  * wait  --  make current process wait on a semaphore
  230.  *-----------------------------------------------------------------------------
  231.  */
  232. SYSCALL    wait(sem)
  233.     int    sem;
  234. {
  235.     char    ps;
  236.     register struct sentry    *sptr;
  237.     register struct pentry    *pptr;
  238.  
  239.     disable(ps);
  240.     if (isbadsem(sem) || (sptr = &semaph[sem])->sstate==SFREE)  {
  241.         restore(ps);
  242.         return(SYSERR);
  243.     }
  244.     if (--(sptr->semcnt) < 0)  {
  245.         (pptr = &proctab[currpid])->pstate = PRWAIT;
  246.         pptr->psem = sem;
  247.         enqueue(currpid,sptr->sqtail);
  248.         resched();
  249.     }
  250.     restore(ps);
  251.     return(OK);
  252. }
  253. /* wakeup.c - wakeup  p. 133 */
  254.  
  255. # include    <conf.h>
  256. # include    <kernel.h>
  257. # include    <proc.h>
  258. # include    <q.h>
  259. # include    <sleep.h>
  260.  
  261. /*-----------------------------------------------------------------------------
  262.  *  wakeup  --  called by clock interrupt dispatcher to awaken processes
  263.  *-----------------------------------------------------------------------------
  264.  */
  265. INTPROC    wakeup()
  266. {
  267.     while (nonempty(clockq) && firstkey(clockq) <= 0)
  268.         ready(getfirst(clockq),RESCHNO);
  269.     if (slnempty = nonempty(clockq))
  270.         sltop = &q[q[clockq].qnext].qkey;
  271.     resched();
  272. }
  273. /* write.c - write  p. 148 */
  274.  
  275. # include    <conf.h>
  276. # include    <kernel.h>
  277. # include    <io.h>
  278.  
  279. /*-----------------------------------------------------------------------------
  280.  *  write  --  write one or more bytes to a device
  281.  *-----------------------------------------------------------------------------
  282.  */
  283. write(descrp, buff, count)
  284.     int    descrp, count;
  285.     char    *buff;
  286. {
  287.     struct devsw    *devptr;
  288.  
  289.     if (isbaddev(descrp))
  290.         return(SYSERR);
  291.     devptr = &devtab[descrp];
  292.     return ((*devptr->dvwrite)(devptr,buff,count));
  293. }
  294. /* xdone.c - xdone  p. 72 */
  295.  
  296. /*-----------------------------------------------------------------------------
  297.  * xdone  --  print system completion message as last process exits
  298.  *-----------------------------------------------------------------------------
  299.  */
  300. xdone()
  301. {
  302.     printf("\n\nAll user processes have completed.\n\n");
  303. }
  304.